package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.utils.TimeLineService;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.MovementTimeLine;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.aspectj.weaver.ast.Var;
import org.bson.types.ObjectId;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@DubboService
public class MovementApiImpl implements  MovementApi{
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private TimeLineService timeLineService;
   //保存
    public String save(Movement movement) {
        //保存
       mongoTemplate.save(movement);
       //多线程
        timeLineService.save(movement);
        return movement.getId().toString();
    }

  //查询个人动态
    public List<Movement> findByUserId(Long userId, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("userId").is(userId).and("state").is(1);
        Query query = Query.query(criteria)
                .with(Sort.by(Sort.Order.desc("created"))) //排序
                .skip((page - 1) * pagesize) //跳过
                .limit(pagesize); //一次查几条
        List<Movement> list = mongoTemplate.find(query, Movement.class);

        return list;
    }

    //查询好友动态
    public List<Movement> findByFriendsId(Long friendId, Integer page, Integer pagesize) {
        //根据friendId 查询关系表
        Criteria criteria = Criteria.where("friendId").is(friendId);
        Query lineQuery = Query.query(criteria).with(Sort.by(Sort.Order.desc("created")))
                .skip((page - 1) * pagesize) //跳过
                .limit(pagesize);//一次查几条
        List<MovementTimeLine> list = mongoTemplate.find(lineQuery, MovementTimeLine.class);
        //提取动态id
        List<Object> ids = CollUtil.getFieldValues(list, "movementId", Object.class);
        //查询动态表
        Criteria criteria1 = Criteria.where("id").in(ids);
        Query query = Query.query(criteria1);
        List<Movement> movements = mongoTemplate.find(query, Movement.class);
        return movements;
    }

    //查询推荐动态
    public List<Movement> findByIds(String[] ids, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("id").in(ids);
        Query query = Query.query(criteria)
                .skip((page - 1) * pagesize)
                .limit(pagesize);
        List<Movement> list = mongoTemplate.find(query, Movement.class);
        return list;
    }

    //随机动态
    public List<Movement> randomMovement(Integer pagesize) {
        //设置采样数量
        TypedAggregation<Movement> aggregation = TypedAggregation.newAggregation(Movement.class, Aggregation.sample(pagesize));

        AggregationResults<Movement> results = mongoTemplate.aggregate(aggregation, Movement.class);
        //从结果中获取最真实数据
        return results.getMappedResults();
    }

    //根据id查询动态
    public Movement findById(String id) {
        Movement movement = mongoTemplate.findById(id, Movement.class);
        return movement;
    }

    //修改状态
    public void updateState(String id, Integer state) {
        Criteria criteria = Criteria.where("id").is(new ObjectId(id));
        Query query = Query.query(criteria);
        Update update = new Update();
        update.set("state",state);
        mongoTemplate.updateFirst(query,update,Movement.class);
    }

    //查询动态列表
    public PageResult findMovements(Integer state, Long uid, Integer page, Integer pagesize) {
        Criteria criteria = new Criteria();
        if (state != null){
            criteria.and("state").is(state);
        }
        if (uid!=null){
            criteria.and("userId").is(uid);
        }
        Query query = Query.query(criteria);
        long count = mongoTemplate.count(query, Movement.class);

        query.with(Sort.by(Sort.Order.desc("created")))
                .skip((page-1)*pagesize)
                .limit(pagesize);
        List<Movement> list = mongoTemplate.find(query, Movement.class);
        return new PageResult(page,pagesize,count,list);
    }
}
